package com.hu.zhcc.shiro.controller.Reponsitory;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.Reponsitory.RSport;
import com.hu.zhcc.shiro.service.Responsitory.SportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 运动项目库Controller
 */
@RestController
public class SportController {

    @Autowired
    private SportService sportService;

    /***
     * 分页及条件查询
     * @param offset
     * @param limit
     * @param portName
     * @return
     */
    @GetMapping("portAll")
    private Page<RSport> portAll(String offset, String limit, String portName) {
        RSport sport = new RSport(portName);
        Map map = new HashMap();
        map.put("offset", offset);
        map.put("limit", limit);
        return sportService.sportAll(map, sport);
    }

    /***
     * 根据id进行删除
     * @param id
     * @return
     */
    @DeleteMapping("deleSport{id}")
    private int deleSport(@PathVariable("id") int id) {
        return sportService.deleteByPrimaryKey(id);
    }

    /***
     * 添加
     * @param sport
     * @return
     */
    @PostMapping("addSport")
    private int addSport(@RequestBody RSport sport) {
        String age = sport.getPortUp() + "-" + sport.getPortDown();
        sport.setPortAge(age);
        sportService.insertSelective(sport);
        Integer id = sport.getId();
        if (id < 10) {
            sport.setPortCoding("YD00" + id);
            return sportService.updateByPrimaryKeySelective(sport);
        } else if (id >= 10) {
            sport.setPortCoding("YD0" + id);
            return sportService.updateByPrimaryKeySelective(sport);
        } else {
            sport.setPortCoding("YD" + id);
            return sportService.updateByPrimaryKeySelective(sport);
        }
    }

    /***
     * 修改前查询
     * @param id
     * @return
     */
    @PostMapping("beforeUpdateByid{id}")
    private RSport beforeUpdateByid(@PathVariable("id") int id) {
        return sportService.beforeSelectByid(id);
    }

    /***
     * 修改
     * @param sport
     * @return
     */
    @PutMapping("updateSportByid")
    private int updateSportByid(@RequestBody RSport sport) {
        return sportService.updateByPrimaryKeySelective(sport);
    }

    /***
     * 查询所有运动名称
     */
    @GetMapping("selectSportNameList")
    public ResponseEntity<List<RSport>> selectSportNameList(){
        List<RSport> sportNameList = sportService.findSportNameList();
        return ResponseEntity.ok(sportNameList);
    }
}
